/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the std::shared_ptrecific language governing permissions and
 * limitations under the License.
 */

#ifndef A_LOOPER_H_
#define A_LOOPER_H_

#include <cstdint>
#include "mutex"
#include "condition_variable"
#include "list"
#include "string"

#define ERROR_BASE              0x100
#define NO_ERROR                0x00
#define UNKNOWN_ERROR           (ERROR_BASE+1)
#define NO_MEMORY               (ERROR_BASE+2)
#define INVALID_OPERATION       (ERROR_BASE+3)
#define BAD_VALUE               (ERROR_BASE+4)
#define BAD_TYPE                (ERROR_BASE+5)
#define NAME_NOT_FOUND          (ERROR_BASE+6)
#define PERMISSION_DENIED       (ERROR_BASE+7)
#define NO_INIT                 (ERROR_BASE+8)
#define ALREADY_EXISTS          (ERROR_BASE+9)
#define DEAD_OBJECT             (ERROR_BASE+10)
#define FAILED_TRANSACTION      (ERROR_BASE+11)
#define BAD_INDEX               (ERROR_BASE+12)
#define NOT_ENOUGH_DATA         (ERROR_BASE+13)
#define WOULD_BLOCK             (ERROR_BASE+14)
#define TIMED_OUT               (ERROR_BASE+15)
#define UNKNOWN_TRANSACTION     (ERROR_BASE+16)
#define FDS_NOT_ALLOWED         (ERROR_BASE+17)

struct AHandler;
struct AMessage;
struct AReplyToken;

struct ALooper : public std::enable_shared_from_this<ALooper>{
public:
    typedef int32_t event_id;
    typedef int32_t handler_id;

    ALooper();
    virtual ~ALooper();

    // Takes effect in a subsequent call to start().
    void setName(const char *name);

    handler_id registerHandler(const std::shared_ptr<AHandler> &handler);
    void unregisterHandler(handler_id handlerID);

    static int64_t GetNowUs();

    const char *getName() const {
        return mName.c_str();
    }

private:
    friend struct AMessage;       // post()

    struct Event {
        int64_t mWhenUs;
        std::shared_ptr<AMessage> mMessage;
    };

    std::mutex mLock;
    std::condition_variable mQueueChangedCondition;

    std::string mName;

    std::list<Event> mEventQueue;

    struct LooperThread;
    std::shared_ptr<LooperThread> mThread;

    // use a separate lock for reply handling, as it is always on another thread
    // use a central lock, however, to avoid creating a mutex for each reply
    std::mutex mRepliesLock;
    std::condition_variable mRepliesCondition;

    // START --- methods used only by AMessage

    // posts a message on this looper with the given timeout
    void post(const std::shared_ptr<AMessage> &msg, int64_t delayUs);

    // creates a reply token to be used with this looper
    std::shared_ptr<AReplyToken> createReplyToken();
    // waits for a reponse for the reply token.  If status is OK, the restd::shared_ptronse
    // is stored into the supplied variable.  Otherwise, it is unchanged.
    int32_t awaitResponse(const std::shared_ptr<AReplyToken> &replyToken, std::shared_ptr<AMessage> &response);
    // posts a reply for a reply token.  If the reply could be successfully posted,
    // it returns OK. Otherwise, it returns an error value.
    int32_t postReply(const std::shared_ptr<AReplyToken> &replyToken, const std::shared_ptr<AMessage> &msg);

    // END --- methods used only by AMessage

    bool loop();

    int32_t start();
    int32_t stop();
};


#endif  // A_LOOPER_H_
